/* 
 * File:   xmlreader.cpp
 * Author: ben
 * 
 * Created on March 15, 2009, 5:35 PM
 */

#include <sstream>


#include "xmlreader.h"
#include "xmlParser.h"

int XmlReader::NormaliseWeights(){
    int i;
    double tot=0;

    for(i=0;i<MyCoeff->getNm();i++){
        tot +=  MyCoeff->getWeight(i);
    }
    if(tot>0){
        for(i=0;i<MyCoeff->getNm();i++){
            MyCoeff->setWeight(i,MyCoeff->getWeight(i)/tot);
            cout << "Normalise:" << MyCoeff->getWeight(i) << endl;
        }
        return(0);
    }
    return(-1);

}

int XmlReader::ReloadWeights(){
    return(ReadWeights(thisname_w));
}

int XmlReader::ReadWeights(string fname){
    string fname_full;
    string smethod,stype,scoeff;
    
    thisname_w=fname;
    
    
    fname_full = thispath + "/" + fname;
    xMainNode = XMLNode::openFileHelper(fname_full.data(), "weights");

    XMLElementPosition idet,ndet,idet1,ndet1;


        idet    =   xMainNode.nAttribute();
        ndet    =   xMainNode.nElement();
        while(idet<ndet){
            XMLNodeContents xncdet=xMainNode.enumContents(idet);
            smethod =   xncdet.child.getName();//NIVEAU FFT, HIST
            idet1   =   xncdet.child.nAttribute();
            ndet1   =   xncdet.child.nElement();
            while(idet1<ndet1){
                XMLNodeContents xncdet1=xncdet.child.enumContents(idet1);
                stype   =   xncdet1.child.getName();
                scoeff  =   xncdet1.child.getAttribute("coeff");
                //cout << smethod+stype << endl;
                //cout << scoeff << endl;
                MyCoeff->putWeight(smethod+stype,scoeff);
                
                idet1++;
            }

            idet++;
        }
    
    
    return(0);
}

double XmlReader::GetPonderatedEdge(int ifrom, int ito){
    int i;
    double tot=0,totp=0;
    for(i=0;i<MyCoeff->getNm();i++){
        tot     +=  MyCoeff->getCoeff(ifrom,ito,i)  *   MyCoeff->getWeight(i);
        totp    +=  MyCoeff->getWeight(i);
    }
    cout << "totaux:" << totp << "," << tot << endl;
    if(totp<0){
        return(-1);
    }
    return(tot/totp);
}

XmlReader::XmlReader(string pathname,string fname){
    string fname_full;
    int n;
    int ifrom,ito;
    string sfrom,sto;
    string smethod,stype,scoeff;

    thisname    =   fname;
    thispath    =   pathname;
    XMLNode temp;
    

    
    
    fname_full = thispath + "/" + thisname;
    xMainNode = XMLNode::openFileHelper(fname_full.data(), "relationships");
    temp = XMLNode::openFileHelper(fname_full.data(), "info");

     XMLNode xNode = xMainNode.getChildNode("relation");
     n = xMainNode.nChildNode("relation");
     //cout << n << endl;
     
     //int ii,nn;
      XMLElementPosition ii=xMainNode.nAttribute(),   // we don't want to list all the attributes
                         nn=xMainNode.nElement();     // there are in total nElement.
      XMLElementPosition idet,ndet,idet1,ndet1;


      //recupere les valeurs
      //cout << "val" << endl;
      //cout << temp.getAttribute("size") << endl;
      //cout << temp.getAttribute("nmethods") << endl;
      MyCoeff   =   new Ncoeff(atoi(temp.getAttribute("nmethods")),atoi(temp.getAttribute("size")));

    while (ii<nn)
    {
        XMLNodeContents xnc=xMainNode.enumContents(ii);
/*
        temp    =   xMainNode.enumContents(ii).child();
        XMLElementPosition iidet=temp.nAttribute(),   // we don't want to list all the attributes
                            nndet=temp.nElement();     // there are in total nElement.
        while(iidet<nndet){
            XMLNodeContents xncdet=xnc.enumContents(iidet);

            cout << xncdet.child.getname() << endl;
        }
 **/
        //cout << xnc.child.getName() << endl;
        sfrom   =   xnc.child.getAttribute("from");
        sto     =   xnc.child.getAttribute("to");

        idet    =   xnc.child.nAttribute();
        ndet    =   xnc.child.nElement();
        while(idet<ndet){
            XMLNodeContents xncdet=xnc.child.enumContents(idet);
            smethod =   xncdet.child.getName();//NIVEAU FFT, HIST
            idet1   =   xncdet.child.nAttribute();
            ndet1   =   xncdet.child.nElement();
            while(idet1<ndet1){
                XMLNodeContents xncdet1=xncdet.child.enumContents(idet1);
                stype   =   xncdet1.child.getName();
                scoeff  =   xncdet1.child.getAttribute("coeff");
                //cout << scoeff << endl;
                //cout << "method" << smethod+stype << endl;
                MyCoeff->addInfo(smethod+stype,scoeff,sfrom,sto);
                //cout << ""
                idet1++;
            }

            idet++;
        }/*
        printf("    Element %i is ",ii);
        switch(xnc.etype)
        {
        case eNodeNULL:
        case eNodeAttribute: break; // in this example we skip the attributes
        case eNodeChild: printf("a childNode named '%s'\n",xnc.child.getName()); break;
        case eNodeText : printf("a text '%s'\n",xnc.text); break;
        case eNodeClear: printf("a '%s' clear Tag\n",xnc.clear.lpszOpenTag);
        }*/
        ii++;
    }

}
int XmlReader::PutInfile(string fname){
    string fname_full;
    string smethod,stype,scoeff;
    ostringstream ss;

    //thisname_w=fname;


    fname_full = thispath + "/" + thisname_w;
    xMainNode = XMLNode::openFileHelper(fname_full.data(), "weights");

    XMLElementPosition idet,ndet,idet1,ndet1;


        idet    =   xMainNode.nAttribute();
        ndet    =   xMainNode.nElement();
        while(idet<ndet){
            XMLNodeContents xncdet=xMainNode.enumContents(idet);
            smethod =   xncdet.child.getName();//NIVEAU FFT, HIST
            idet1   =   xncdet.child.nAttribute();
            ndet1   =   xncdet.child.nElement();
            while(idet1<ndet1){
                XMLNodeContents xncdet1=xncdet.child.enumContents(idet1);
                stype   =   smethod+xncdet1.child.getName();
                //scoeff  =   xncdet1.child.getAttribute("coeff");
                ss.str("");
                ss << MyCoeff->getWeight(stype);
                cout << "stype:" << stype << endl;
                cout << "ss:" << ss.str().data() << endl;
                xncdet1.child.updateAttribute(ss.str().data(),"coeff");


                //cout << smethod+stype << endl;
                //cout << scoeff << endl;
                //MyCoeff->putWeight(smethod+stype,scoeff);

                idet1++;
            }

            idet++;
        }

    fname_full=thispath+"/"+fname;
    xMainNode.writeToFile(fname_full.data(),"ISO-8859-1");
    return(0);
}
XmlReader::~XmlReader(){
    delete(MyCoeff);
}

